<template>
  <div>

    <el-table ref="multipleTable" :data="cartList" tooltip-effect="dark" style="width: 100%"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection"  width="55"></el-table-column>
      <el-table-column prop="productId" label="skuId" width="180"></el-table-column>
      <el-table-column prop="productName" label="名称" width="180"></el-table-column>
      <el-table-column prop="productPrice" label="单价" width="180"></el-table-column>
      <el-table-column prop="payNum" label="购买数量" width="180"></el-table-column>
      <el-table-column prop="totalPrice" label="总价" width="180"></el-table-column>
    </el-table>
    {{'总数量:'+totalNum}}
    {{'总价钱:'+settleAccountsParam.totalPrice}}
    <el-button type="primary" @click="settleAccounts" plain>结算</el-button>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）,
//例如：import 《组件名称》 from '《组件路径》,
import {checkCart, getCartList} from "@/api/cart/cart";

export default {
  //import引入的组件需要注入到对象中才能使用"
  components: {},
  props: {},
  data() {
    //这里存放数据"

    return {
      cartList: [],
      skuIdArr: [],
      settleAccountsParam:{
        totalPrice: 0,
        skuIds:''
      },
      totalNum: 0
    };
  },
  //计算属性 类似于data概念",
  computed: {},
  //监控data中的数据变化",
  watch: {},
  //方法集合",
  methods: {
    getCartList() {
      getCartList().then(res => {
        this.cartList = res.data
      })
    },
    settleAccounts(){
      if (this.settleAccountsParam.skuIds.length == 0){
        this.$message.warning("请选择要购买的商品")
        return;
      }
      localStorage.setItem("totalPrice",this.settleAccountsParam.totalPrice)
      localStorage.setItem("skuIds",this.settleAccountsParam.skuIds)
      this.$router.push({
        name: 'SettleAccounts',
        params: {
          totalPrice: this.settleAccountsParam.totalPrice,
          skuIds: this.settleAccountsParam.skuIds
        }
      });
    },
    handleSelectionChange(val) {
      this.skuIdArr = val;
      var skuIds = '';
      this.skuIdArr.forEach(sku=>{
        skuIds+=','+sku.productId
      })
      skuIds = skuIds.substring(1)
      console.log(skuIds);
      if(skuIds.length == 0){
        this.settleAccountsParam.totalPrice = 0.00
        this.totalNum = 0
        this.settleAccountsParam.skuIds = ''
        return;
      }
      checkCart(skuIds).then(res => {
        this.settleAccountsParam.totalPrice = res.data.totalPrice
        this.totalNum = res.data.totalNum
        this.settleAccountsParam.skuIds = skuIds
      })
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）",
  created() {
    this.getCartList()
  },
  //生命周期 - 挂载完成（可以访问DOM元素）",
  mounted() {
  },
  beforeCreate() {
  }, //生命周期 - 创建之前",
  beforeMount() {
  }, //生命周期 - 挂载之前",
  beforeUpdate() {
  }, //生命周期 - 更新之前",
  updated() {
  }, // 生命周期 - 更新之后",
  beforeDestroy() {
  }, // 生命周期 - 销毁之前",
  destroyed() {
  }, // 生命周期 - 销毁完成",
  activated() {
  } // 如果页面有keep-alive缓存功能，这个函数会触发",
};
</script>
<style scoped>

</style>
